package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.dao.TxnLimitCfgMapper;
import cc.rengu.igas.bomp.common.dao.impl.TxnLimitCfgMapperImpl;
import cc.rengu.igas.bomp.common.entity.TxnLimitCfg;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.TxnLimitCfgBean;
import cc.rengu.igas.bomp.facade.request.UserLimitManageRequest;
import cc.rengu.igas.bomp.facade.response.UserLimitManageResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.*;
import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户限额管理
 * Created by wangcheng on 2021/7/19 15:27
 */
public class UserLimitManageService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        UserLimitManageRequest userLimitManageRequest = new UserLimitManageRequest();
        ConvertUtil.convertOutput(userLimitManageRequest);
        return userLimitManageRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        UserLimitManageRequest userLimitManageRequest = (UserLimitManageRequest) request;
        String oprType = userLimitManageRequest.getOprType();

        /* 参数合法性校验 */
        if (StringUtil.isEmptyOrNull(oprType) || null == userLimitManageRequest.getTxnLimitList()) {
            rglog.error("用户操作类型:<{}>或者用户限额:<{}>为空", oprType, JSON.toJSONString(userLimitManageRequest.getTxnLimitList()));
            throw new BizException(RespCodeEnum.PARAM_ERROR.getRespCode(), RespCodeEnum.PARAM_ERROR.getRespDesc());
        }

        /* 转换用户限额配置信息 */
        List<TxnLimitCfg> txnLimitCfgList = new ArrayList<>();
        if (null != userLimitManageRequest.getTxnLimitList()) {
            for (TxnLimitCfgBean txnLimitCfgBean : userLimitManageRequest.getTxnLimitList()) {
                TxnLimitCfg txnLimitCfg = new TxnLimitCfg();
                BeanUtil.beanCopy(txnLimitCfgBean, txnLimitCfg);
                txnLimitCfgList.add(txnLimitCfg);
            }
        }

        /* 用户权限信息管理 */
        int iReturnCode;
        TxnLimitCfgMapper txnLimitCfgMapper = new TxnLimitCfgMapperImpl();
        switch (oprType) {
            case "00":
                for (TxnLimitCfg txnLimitCfg : txnLimitCfgList) {
                    txnLimitCfg.setId(RandomUtil.getUUID());
                    txnLimitCfg.setStatus(AppParamConstant.YES);
                    txnLimitCfg.setAuditStatus("02");
                    txnLimitCfg.setOprId(userLimitManageRequest.getHeader().getChanlId());
                    txnLimitCfg.setCreateTime(DateUtil.getCurrentDateTime("YYYY-MM-DD hh:mm:ss.SSS"));
                    iReturnCode = txnLimitCfgMapper.insertTxnLimitCfg(txnLimitCfg);
                    if (0 != iReturnCode) {
                        rglog.error("登记交易限额配置信息失败!");
                        throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
                    }
                }
                break;
            case "01":
                for (TxnLimitCfg txnLimitCfg : txnLimitCfgList) {
                    TxnLimitCfg txnLimitCfgTmp = txnLimitCfgMapper.selectTxnLimitCfgByPrimaryKey(txnLimitCfg.getInstId(), txnLimitCfg.getLimitType(), txnLimitCfg.getLimitInst(), txnLimitCfg.getLimitAcctType());
                    if (null != txnLimitCfgTmp) {
                        BeanUtil.beanCopy(txnLimitCfgTmp, txnLimitCfg);
                        txnLimitCfg.setLastUpdateTime(DateUtil.getCurrentDateTime("YYYY-MM-DD hh:mm:ss.SSS"));
                        iReturnCode = txnLimitCfgMapper.updateTxnLimitCfgByPrimaryKey(txnLimitCfg);
                        if (0 != iReturnCode) {
                            rglog.error("更新交易限额配置信息失败!");
                            throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
                        }
                    }
                }
                break;
            case "02":
                for (TxnLimitCfg txnLimitCfg : txnLimitCfgList) {
                    TxnLimitCfg txnLimitCfgTmp = txnLimitCfgMapper.selectTxnLimitCfgByPrimaryKey(txnLimitCfg.getInstId(), txnLimitCfg.getLimitType(), txnLimitCfg.getLimitInst(), txnLimitCfg.getLimitAcctType());
                    if (null != txnLimitCfgTmp) {
                        BeanUtil.beanCopy(txnLimitCfgTmp, txnLimitCfg);
                        txnLimitCfg.setStatus(AppParamConstant.NO);
                        txnLimitCfg.setLastUpdateTime(DateUtil.getCurrentDateTime("YYYY-MM-DD hh:mm:ss.SSS"));
                        iReturnCode = txnLimitCfgMapper.updateTxnLimitCfgByPrimaryKey(txnLimitCfg);
                        if (0 != iReturnCode) {
                            rglog.error("更新交易限额配置信息失败!");
                            throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
                        }
                    }
                }
                break;
            default:
                rglog.error("不支持的用户信息查询方式:<{}>", oprType);
                throw new BizException(RespCodeEnum.NOT_SUPPORT_ORT_TYPE.getRespCode(), RespCodeEnum.NOT_SUPPORT_ORT_TYPE.getRespDesc());
        }

        /* 组装应答报文 */
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        UserLimitManageResponse userLimitManageResponse = (UserLimitManageResponse) bizResponse.getResult();
        ConvertUtil.convertInput(userLimitManageResponse);
    }
}
